#!/bin/bash

# curl https://gitee.com/lgf1244/scripts/raw/main/shell/system-init.sh | bash

set -e

# curl https://gitee.com/lgf1244/scripts/raw/main/shell/system-init.sh | bash

# -e ：判断文件或目录是否存在
# -d ：判断是不是目录以及是否存在
# -f ：判断是不是普通文件以及是否存在
# -r ：判断是否有读权限
# -w ：判断是否有写权限
# -x ：判断是否有执行权限

# 字体颜色
# echo -e "\e[30m 黑色 \e[0m"
# echo -e "\e[31m 红色 \e[0m"
# echo -e "\e[32m 绿色 \e[0m"
# echo -e "\e[33m 黄色 \e[0m"
# echo -e "\e[34m 蓝色 \e[0m"
# echo -e "\e[35m 紫色 \e[0m"
# echo -e "\e[36m 青色 \e[0m"
# echo -e "\e[37m 白色 \e[0m"

info_red(){
  echo -e "\e[31m $* \e[0m"
}
info_green(){
  echo -e "\e[32m $* \e[0m"
}
info_yellow(){
  echo -e "\e[33m $* \e[0m"
}
info_blue(){
  echo -e "\e[34m $* \e[0m"
  # printf "\e[34m $@\n\e[0m"
}
info_pink(){
  echo -e "\e[35m $* \e[0m"
}
info_sky(){
  echo -e "\e[36m $* \e[0m"
}
# 背景颜色
# echo -e "\e[40m 黑底 \e[0m"
# echo -e "\e[41m 红底 \e[0m"
# echo -e "\e[42m 绿底 \e[0m"
# echo -e "\e[43m 黄底 \e[0m"
# echo -e "\e[44m 蓝底 \e[0m"
# echo -e "\e[45m 紫底 \e[0m"
# echo -e "\e[46m 青底 \e[0m"
# echo -e "\e[47m 白底 \e[0m"


command_exists() {
  command -v "$@" >/dev/null 2>&1
}

get_distribution() {
  lsb_dist=""
  # Every system that we officially support has /etc/os-release
  if [ -r /etc/os-release ]; then
    lsb_dist="$(. /etc/os-release && echo "$ID")"
  fi
  # Returning an empty string here should be alright since the
  # case statements don't act unless you provide an actual value
  echo "$lsb_dist"
}

if [ "$(whoami)" = "root" ]; then
  info_blue "this is root account"
  sudo_flag=""
else
  info_blue "this is non-root account"
  sudo_flag="sudo"
fi

all_pkgs=""
lsb_dist=""
sub_lsb_dist=""
if [ -r /etc/os-release ]; then
  lsb_dist="$(. /etc/os-release && echo "$ID")"
  sub_lsb_dist="$(. /etc/os-release && echo "$VERSION_ID")"
  echo ""
  echo ""
  echo ""
  info_sky "init $lsb_dist-$sub_lsb_dist system now!!!"
  echo ""
  echo ""
  echo ""
fi

$sudo_flag ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' | $sudo_flag tee /etc/timezone
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# timedatectl set-timezone Asia/Shanghai

debian_like_common_pkgs="apt-utils \
  locales \
  debconf \
  lxc \
  iptables \
  dmsetup \
  openssh-client \
  gnupg2 \
  dirmngr \
  iproute2 \
  procps \
  lsof \
  htop \
  net-tools \
  psmisc \
  rsync \
  ca-certificates \
  unzip \
  zip \
  less \
  jq \
  lsb-release \
  apt-transport-https \
  dialog \
  libc6 \
  libgcc1 \
  libkrb5-3 \
  libgssapi-krb5-2 \
  libicu[0-9][0-9] \
  liblttng-ust[0-9] \
  libstdc++6 \
  zlib1g \
  locales \
  sudo \
  dialog \
  ncdu \
  man-db \
  strace \
  manpages \
  manpages-dev \
  init-system-helpers
"

alpine_like_common_pkgs="openssh-client \
  git \
  gnupg \
  procps \
  lsof \
  htop \
  net-tools \
  psmisc \
  rsync \
  ca-certificates \
  unzip \
  zip \
  less \
  jq \
  libgcc \
  libstdc++ \
  krb5-libs \
  libintl \
  libssl1.1 \
  lttng-ust \
  tzdata \
  userspace-rcu \
  zlib \
  sudo \
  coreutils \
  sed \
  grep \
  which \
  ncdu \
  shadow \
  strace \
  musl-locales \
  musl-locales-lang
"

arch_like_common_pkgs="git \
  openssh \
  vim \
  neovim \
  nano \
  sudo
"

opensuse_like_common_pkgs="git \
  openssh \
  nano \
  vim \
  neovim \
  emacs \
  sudo
"

nixos_like_common_pkgs="git \
  nano \
  vim \
  emacs \
  bash \
  zsh \
  fish \
  tmux \
  sudo
"

rhel_like_common_pkgs="git \
  openssh \
  util-linux-user \
  glibc-common \
  vim \
  nano \
  bash \
  zsh \
  tmux \
  sudo
"

cygwin_like_common_pkgs="vim \
  nano
"

freebsd_like_common_pkgs="vim \
  nano
"

if [ "$lsb_dist" = "debian" ]; then
  $sudo_flag  cp /etc/apt/sources.list /etc/apt/sources.list.bak
  # $sudo_flag  apt-get update && apt-get install -y  debconf ca-certificates apt-transport-https
  $sudo_flag  sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
  $sudo_flag  sed -i 's/security.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
  # sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
  if [ "$sub_lsb_dist" = "9" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump ranger \
          curl wget axel aria2"
  elif [ "$sub_lsb_dist" = "10" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump nnn ranger \
          curl wget axel aria2"
  elif [ "$sub_lsb_dist" = "11" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump nnn fzf ranger \
          curl wget axel aria2"
  fi
  all_pkgs="${debian_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "kali" ]; then
  $sudo_flag  cp /etc/apt/sources.list /etc/apt/sources.list.bak
  echo "deb https://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib" | $sudo_flag  tee /etc/apt/sources.list
  pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump nnn ranger \
          curl wget axel aria2"
  all_pkgs="${debian_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "ubuntu" ]; then
  $sudo_flag  cp /etc/apt/sources.list /etc/apt/sources.list.bak
  $sudo_flag  apt-get update && $sudo_flag  apt-get install -y  ca-certificates apt-transport-https
  $sudo_flag  sed -i "s@http://.*archive.ubuntu.com@https://mirrors.ustc.edu.cn@g" /etc/apt/sources.list
  $sudo_flag  sed -i "s@http://.*security.ubuntu.com@https://mirrors.ustc.edu.cn@g" /etc/apt/sources.list
  # sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
  # sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
  if [ "$sub_lsb_dist" = "14.04" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump ranger \
          curl wget axel aria2 \
          gcc g++ clang gdb cmake make automake \
          python3 python3-dev python3-pip python3-venv"
  elif [ "$sub_lsb_dist" = "16.04" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs\
          autojump ranger \
          curl wget axel aria2 \
          gcc g++ clang gdb cmake make automake \
          python3 python3-dev python3-pip python3-venv"
  elif [ "$sub_lsb_dist" = "18.04" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump nnn ranger \
          curl wget axel aria2 \
          gcc g++ clang gdb cmake make automake \
          python3 python3-dev python3-pip python3-venv"
  elif [ "$sub_lsb_dist" = "20.04" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump nnn fzf ranger \
          curl wget axel aria2 \
          fzf silversearcher-ag fd-find nnn ranger jq bat \
          gcc g++ clang gdb cmake make automake \
          python3 python3-dev python3-pip python3-venv pipx"
  elif [ "$sub_lsb_dist" = "22.04" ]; then
    pkgs="bash zsh fish csh tcsh ksh tmux \
          git openssh-server vim nano emacs \
          autojump nnn fzf ranger \
          curl wget axel aria2 \
          fzf silversearcher-ag fd-find nnn ranger jq bat \
          gcc g++ clang gdb cmake make automake \
          python3 python3-dev python3-pip python3-venv pipx"
  fi
  all_pkgs="${debian_like_common_pkgs} ${pkgs}"
  # echo $all_pkgs
fi

if [ "$lsb_dist" = "arch" ]; then
  #Server = http://mirrors.163.com/archlinux/$repo/os/$arch
  #Server = http://mirrors.aliyun.com/archlinux/$repo/os/$arch
  #Server = https://mirrors.aliyun.com/archlinux/$repo/os/$arch
  #Server = http://mirrors.bfsu.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.bfsu.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.cqu.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.cqu.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.dgut.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.dgut.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.hit.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.hit.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirror.lzu.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.neusoft.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.neusoft.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.nju.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.nju.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.njupt.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirror.redrock.team/archlinux/$repo/os/$arch
  #Server = https://mirror.redrock.team/archlinux/$repo/os/$arch
  #Server = http://mirrors.shanghaitech.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.shanghaitech.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.sjtug.sjtu.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.wsyu.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.wsyu.edu.cn/archlinux/$repo/os/$arch
  #Server = https://mirrors.xjtu.edu.cn/archlinux/$repo/os/$arch
  #Server = http://mirrors.zju.edu.cn/archlinux/$repo/os/$arch

  echo "Server = http://mirrors.ustc.edu.cn/archlinux/\$repo/os/\$arch" | $sudo_flag  tee /etc/pacman.d/mirrorlist
  echo "Server = https://mirrors.ustc.edu.cn/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = http://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = https://mirrors.aliyun.com/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  cat /etc/pacman.d/mirrorlist

  FIND_FILE="/etc/pacman.conf"
  FIND_STR="[archlinuxcn]"
  # 判断匹配函数，匹配函数不为0，则包含给定字符
  if [ `grep -c "$FIND_STR" $FIND_FILE` -ne '0' ];then
    echo "" | $sudo_flag  tee -a /etc/pacman.conf
    echo "[archlinuxcn]" | $sudo_flag  tee -a /etc/pacman.conf
    echo "Server = https://mirrors.ustc.edu.cn/archlinuxcn/\$arch" | $sudo_flag  tee -a /etc/pacman.conf
  fi
  cat /etc/pacman.conf

  # $sudo_flag  pacman -S eflector
  # $sudo_flag  eflector --sort rate --country China --protocol https --latest 5 --save /etc/pacman.d/mirrorlist
  # $sudo_flag  reflector \
  #   --country China \
  #   --protocol https \
  #   --latest 5 \
  #   --save /etc/pacman.d/mirrorlist
  $sudo_flag  pacman-key --init
  $sudo_flag  pacman-key --populate
  $sudo_flag  pacman -Sy archlinux-keyring  --noconfirm
  $sudo_flag  pacman -Su --noconfirm

  pkgs="which base git wget nano curl dnsutils openssh make gcc net-tools inetutils \
        traceroute zsh bash zsh fish tcsh ksh tmux vim neovim nano emacs"
  all_pkgs="${arch_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "manjaro" ]; then
  # manjaro 源
  echo "Server = https://mirrors.ustc.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee /etc/pacman.d/mirrorlist
  echo "Server = https://mirrors.tuna.tsinghua.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = https://mirrors.sjtug.sjtu.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = https://mirrors.zju.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # # arch 源
  echo "Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = http://mirrors.163.com/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  echo "Server = http://mirrors.aliyun.com/archlinux/\$repo/os/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist

  # echo "[archlinuxcn]" | $sudo_flag  tee -a /etc/pacman.conf
  # echo "SigLevel = Optional TrustAll" | $sudo_flag  tee -a /etc/pacman.conf
  # echo "Server = https://mirrors.ustc.edu.cn/archlinuxcn/$arch" | $sudo_flag  tee -a /etc/pacman.conf

  # $sudo_flag  pacman-mirrors -i -c China -m rank

  # echo "## Country : China" | $sudo_flag  tee /etc/pacman.d/mirrorlist
  # echo "Server = https://mirrors.tuna.tsinghua.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # echo "" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # echo "## Country : China" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # echo "Server = https://mirrors.tuna.tsinghua.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # echo "" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # echo "## Country : China" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  # echo "Server = https://mirrors.sjtug.sjtu.edu.cn/manjaro/stable/\$repo/\$arch" | $sudo_flag  tee -a /etc/pacman.d/mirrorlist
  pkgs="which base git wget nano curl dnsutils openssh make gcc net-tools inetutils \
        traceroute zsh bash zsh fish tcsh ksh tmux vim neovim nano emacs"
  all_pkgs="${arch_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "fedora" ]; then
  pkgs="fish vim neovim nano emacs"
  all_pkgs="${rhel_like_common_pkgs} ${pkgs}"
fi

# centos / centos-stream
if [ "$lsb_dist" = "centos" ]; then
  # centos6 error on WSL2
  if [[ $sub_lsb_dist =~ 6  ]]; then
    pkgs="emacs"
  elif [[ $sub_lsb_dist =~ 7 ]]; then
    pkgs="emacs"
  # centos8 源用不了
  elif [[ $sub_lsb_dist =~ 8 ]]; then
    # cd /etc/yum.repos.d/
    # rm -rf *.repo
    # curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    # # wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    # yum makecache
    pkgs="emacs"
  elif [[ $sub_lsb_dist =~ 9 ]]; then
    pkgs="emacs"
  fi
  all_pkgs="${rhel_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "rocky" ]; then
  pkgs="vim nano emacs"
  all_pkgs="${rhel_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "almalinux" ]; then
  # failed install fish on almalinux
  # $sudo_flag  dnf config-manager --add-repo https://download.opensuse.org/repositories/shells:/fish:/release:/3/CentOS_8/shells:fish:release:3.repo
  # $sudo_flag  dnf install fish -y
  pkgs="emacs"
  all_pkgs="${rhel_like_common_pkgs} ${pkgs}"
fi

# oraclelinux
# ./shell.sh: line 6: [: =~: binary operator expected : https://unix.stackexchange.com/questions/659038/shell-sh-line-6-binary-operator-expected
# shell if [[ ]]和[ ]区别 || && : https://www.cnblogs.com/aaron-agu/p/5700650.html
if [ "$lsb_dist" = "ol" ]; then
  if [[ $sub_lsb_dist =~ 7  ]]; then
    pkgs="fish emacs"
  elif [[ $sub_lsb_dist =~ 8 ]]; then
    pkgs="emacs"
  elif [[ $sub_lsb_dist =~ 9 ]]; then
    pkgs="emacs"
  fi
  all_pkgs="${rhel_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "openEuler" ]; then
  if [ "$sub_lsb_dist" = "20.03" ]; then
    pkgs="fish emacs"
  elif [ "$sub_lsb_dist" = "22.03" ]; then
    pkgs="fish emacs"
  fi
  all_pkgs="${rhel_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "opensuse-leap" ]; then
  pkgs="bash zsh fish tcsh ksh tmux"
  all_pkgs="${opensuse_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "opensuse-tumbleweed" ]; then
  pkgs="bash zsh fish tcsh ksh tmux"
  all_pkgs="${opensuse_like_common_pkgs} ${pkgs}"
fi

# Linux shell：判断变量是否包含某个字符串的几种方法: https://blog.csdn.net/weixin_44498318/article/details/109351238
if [ "$lsb_dist" = "alpine" ]; then
  $sudo_flag cp /etc/apk/repositories /etc/apk/repositories.bak
  $sudo_flag sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
  if [[ $sub_lsb_dist =~ 3.15 ]]; then
    echo "alpine-3.15"
    pkgs="shadow git bash zsh tmux fish tcsh openssh-server net-tools vim nano neovim emacs \
          curl wget fzf nnn ranger musl-locales musl-locales-lang \
          perl ruby"
  elif [[ $sub_lsb_dist =~ 3.16 ]]; then
    echo "alpine-3.16"
    pkgs="shadow git bash zsh tmux fish tcsh openssh-server net-tools vim nano neovim emacs \
          curl wget fzf nnn ranger musl-locales musl-locales-lang \
          perl ruby"
  elif [[ $sub_lsb_dist =~ 3.17 ]]; then
    echo "alpine-3.17"
    pkgs="shadow git bash zsh tmux fish tcsh openssh-server net-tools vim nano neovim emacs \
          curl wget fzf nnn ranger musl-locales musl-locales-lang \
          perl ruby"
  fi
  all_pkgs="${alpine_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "msys2" ]; then
  pkgs="vim emacs nano"
  all_pkgs="${cygwin_like_common_pkgs} ${pkgs}"
fi

if [ "$lsb_dist" = "cygwin" ]; then
  pkgs="vim emacs nano"
  all_pkgs="${cygwin_like_common_pkgs} ${pkgs}"
fi


echo "Packages to verify are installed: $all_pkgs"
case "$lsb_dist" in
  debian | ubuntu | kali | zorin | mint | deepin)
    $sudo_flag  apt-get update
    $sudo_flag  apt-get upgrade -y
    # Install libssl1.1 if available
    if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
      $sudo_flag  apt-get install -y libssl1.1
    fi

    # Install appropriate version of libssl1.0.x if available
    libssl_package=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
    if [ "$(echo "$LIlibssl_packageBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
        if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
            # Debian 9
            $sudo_flag  apt-get install -y libssl1.0.2
        elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
            # Ubuntu 18.04, 16.04, earlier
            $sudo_flag  apt-get install -y libssl1.0.0
        fi
    fi
    $sudo_flag apt-get -y install --no-install-recommends ${all_pkgs}
    $sudo_flag apt-get autoremove --purge -y
    $sudo_flag apt-get clean -y
    $sudo_flag rm -rf /var/lib/apt/lists/*

    $sudo_flag locale-gen en_US.UTF-8
    # locale-gen en_US.UTF-8
    echo 'LANG="en_US.UTF-8"' | $sudo_flag tee /etc/default/locale

    # # Ensure at least the en_US.UTF-8 UTF-8 locale is available.
    # # Common need for both applications and things like the agnoster ZSH theme.
    # if ! grep -o -E '^\s*en_US.UTF-8\s+UTF-8' /etc/locale.gen > /dev/null; then
    #     echo "en_US.UTF-8 UTF-8" | $sudo_flag tee -a /etc/locale.gen
    #     $sudo_flag locale-gen
    #     LOCALE_ALREADY_SET="true"
    # fi
    # cat /etc/locale.gen
    ;;
  arch | manjaro)
    $sudo_flag  pacman -Syyu
    $sudo_flag  pacman -Syy --noprogressbar --noconfirm --needed ${all_pkgs}
    ;;
  fedora | centos | rhel | almalinux | rocky | ol | openEuler)
    $sudo_flag  yum update -y
    $sudo_flag  yum -y install ${all_pkgs}
    # $sudo_flag  localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
    # [error] character map file `UTF-8' not found: No such file or directory
    ;;
  opensuse-leap | opensuse-tumbleweed)
    $sudo_flag  zypper --gpg-auto-import-keys --non-interactive refresh
    $sudo_flag  zypper --gpg-auto-import-keys --non-interactive update
    $sudo_flag  zypper --gpg-auto-import-keys --non-interactive install --auto-agree-with-licenses ${all_pkgs}
    ;;
  alpine)
    $sudo_flag apk update
    $sudo_flag apk add --no-cache ${all_pkgs}
    ;;
  darwin*)
    brew install ${all_pkgs}
    ;;
  cygwin | msys2 | win32)
    pacman -Syy --noprogressbar --noconfirm --needed ${all_pkgs}
    ;;
  *)
    printf '%s\n' "Unknown OS detected, aborting..." >&2
    exit 1
    ;;
esac




# https://docs.docker.com/engine/install/ubuntu/
install_docker(){
  # curl -sSL https://get.docker.com/ | sh  # only support ubuntu and debian distri
  case "$lsb_dist" in
    ubuntu)
      info_green "install docker on ubuntu"
      # $sudo_flag apt-get remove -y docker docker-engine docker.io containerd runc
      $sudo_flag apt-get update
      $sudo_flag apt-get install -y ca-certificates curl gnupg lsb-release
      $sudo_flag mkdir -p /etc/apt/keyrings
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | $sudo_flag gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
        $(lsb_release -cs) stable" | $sudo_flag tee /etc/apt/sources.list.d/docker.list > /dev/null
      $sudo_flag chmod a+r /etc/apt/keyrings/docker.gpg
      $sudo_flag apt-get update
      $sudo_flag apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-compose
      ;;
    debian)
      info_green "install docker on debian"
      # $sudo_flag apt-get remove -y docker docker-engine docker.io containerd runc
      $sudo_flag apt-get update
      $sudo_flag apt-get install -y ca-certificates curl gnupg lsb-release
      $sudo_flag mkdir -p /etc/apt/keyrings
      curl -fsSL https://download.docker.com/linux/debian/gpg | $sudo_flag gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
        $(lsb_release -cs) stable" | $sudo_flag tee /etc/apt/sources.list.d/docker.list > /dev/null
      $sudo_flag chmod a+r /etc/apt/keyrings/docker.gpg
      $sudo_flag apt-get update
      $sudo_flag apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-compose
      ;;
    kali)
      info_green "install docker on kali"
      # https://www.kali.org/docs/containers/installing-docker-on-kali/
      # $sudo_flag apt-get update && $sudo_flag apt-get install -y docker.io docker-compose podman
      printf '%s\n' "deb https://download.docker.com/linux/debian bullseye stable" |
        $sudo_flag tee /etc/apt/sources.list.d/docker-ce.list
      curl -fsSL https://download.docker.com/linux/debian/gpg |
        $sudo_flag gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-ce-archive-keyring.gpg
      $sudo_flag apt-get update && $sudo_flag apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose podman
      ;;
    alpine)
      info_green "install docker on alpine"
      $sudo_flag apk update
      $sudo_flag apk add --no-cache docker docker-compose podman openrc lxc musl-dev
      $sudo_flag rc-update add docker boot
      # $sudo_flag service docker start
      ;;
    centos)
      info_green "install docker on centos"
      # $sudo_flag yum remove docker \
      #     docker-client \
      #     docker-client-latest \
      #     docker-common \
      #     docker-latest \
      #     docker-latest-logrotate \
      #     docker-logrotate \
      #     docker-engine
      $sudo_flag yum install -y yum-utils
      $sudo_flag yum-config-manager \
          --add-repo \
          https://download.docker.com/linux/centos/docker-ce.repo
      $sudo_flag yum install -y --nobest --allowerasing --skip-broken docker-ce docker-ce-cli containerd.io docker-compose-plugin podman
      ;;
    fedora | almalinux | rocky | ol | openEuler)
      info_green "install docker on fedora"
      # $sudo_flag dnf remove -y docker \
      #     docker-client \
      #     docker-client-latest \
      #     docker-common \
      #     docker-latest \
      #     docker-latest-logrotate \
      #     docker-logrotate \
      #     docker-selinux \
      #     docker-engine-selinux \
      #     docker-engine
      $sudo_flag dnf -y install dnf-plugins-core
      $sudo_flag dnf config-manager \
          --add-repo \
          https://download.docker.com/linux/fedora/docker-ce.repo
      $sudo_flag dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin podman
      ;;
    rhel)
      info_green "install docker on rhel"
      # $sudo_flag yum remove docker \
      #   docker-client \
      #   docker-client-latest \
      #   docker-common \
      #   docker-latest \
      #   docker-latest-logrotate \
      #   docker-logrotate \
      #   docker-engine \
      #   podman \
      #   runc
      $sudo_flag yum install -y yum-utils
      $sudo_flag yum-config-manager \
          --add-repo \
          https://download.docker.com/linux/rhel/docker-ce.repo
      $sudo_flag yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin podman
      ;;
    opensuse-leap | opensuse-tumbleweed)
      info_green "install docker on opensuse"
      $sudo_flag  zypper --gpg-auto-import-keys --non-interactive refresh
      $sudo_flag  zypper --gpg-auto-import-keys --non-interactive update
      $sudo_flag  zypper --gpg-auto-import-keys --non-interactive install --auto-agree-with-licenses docker docker-compose podman
      ;;
    arch | manjaro)
      info_green "install docker on arch-like os"
      $sudo_flag  pacman -Syyu
      $sudo_flag  pacman -Syy --noprogressbar --noconfirm --needed docker docker-compose podman
      ;;
    sles)
      info_green "install docker on sles"
      # opensuse_repo="https://download.opensuse.org/repositories/security:SELinux/SLE_15_SP3/security:SELinux.repo"
      # $sudo_flag zypper addrepo $opensuse_repo
      opensuse_repo="https://download.opensuse.org/repositories/security:SELinux/15.4/security:SELinux.repo"
      $sudo_flag zypper addrepo $opensuse_repo
      # $sudo_flag zypper remove docker \
      #   docker-client \
      #   docker-client-latest \
      #   docker-common \
      #   docker-latest \
      #   docker-latest-logrotate \
      #   docker-logrotate \
      #   docker-engine \
      #   runc
      $sudo_flag zypper addrepo https://download.docker.com/linux/sles/docker-ce.repo
      $sudo_flag zypper install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin podman
      ;;
    darwin*)
      brew install ${all_pkgs}
      ;;
    *)
      printf '%s\n' "docker not support lsb_dist OS, aborting..." >&2
      ;;
  esac
  $sudo_flag  usermod -a -G docker $(whoami)
  $sudo_flag  gpasswd -a $(whoami) docker
  if [ -f /.dockerenv ]; then
    info_blue "docker-in-dokcer setup"
    if [ -x /usr/local/bin/dind ]; then
      info_blue "docker-in-docker has been setup"
    else
      curl -L https://gitee.com/lgf1244/scripts/raw/main/shell/dind -o ./dind
      $sudo_flag mv ./dind /usr/local/bin
      $sudo_flag chmod +x /usr/local/bin/dind
    fi
  else
    info_blue "dokcer setup"
    if command_exists systemctl; then
      $sudo_flag  systemctl enable --now docker
    fi
  fi

  # $sudo_flag  gpasswd -a $(whoami) sudo
}

install_docker_compose(){
  info_green '安装Docker-compose......'
  # https://ghproxy.com/https://github.com/docker/compose/releases/download/v2.15.1/checksums.txt

  curl -L https://ghproxy.com/https://github.com/docker/compose/releases/download/v2.15.0/docker-compose-`uname -s`-`uname -m` -o ./docker-compose
  $sudo_flag mv ./docker-compose /usr/local/bin
  $sudo_flag chmod +x /usr/local/bin/docker-compose
  # curl -L https://gitee.com/lgf1244/scripts/raw/main/shell/dind -o ./dind
  # $sudo_flag mv ./dind /usr/local/bin
  # $sudo_flag chmod +x /usr/local/bin/dind

}

if command_exists docker && [ -e /var/run/docker.sock ]; then
  info_sky "docker has been installed"
  # docker version
else
  install_docker
fi

if command_exists docker-compose; then
  info_sky "docker-compose has been installed"
  # docker-compose version
else
  install_docker_compose
fi

install_vagrant()
{
  info_green "install vagrant on $lsb_dist now!!!"
}

install_homebrew()
{
  info_green "install homebrew on $lsb_dist now!!!"
}

install_fish(){
  info_green "compile fish on $lsb_dist now!!!"
  case "$lsb_dist" in
    debian | ubuntu | kali | zorin | mint | deepin)
      $sudo_flag apt-get update
      $sudo_flag apt-get install -y build-essential cmake ncurses-dev libncurses5-dev libpcre2-dev gettext
      ;;
    fedora | centos | rhel | almalinux | rocky | ol | openEuler)
      $sudo_flag yum install -y ncurses-devel
      ;;
    arch | manjaro)
      pacman -Syy --noprogressbar --noconfirm --needed fish
      ;;
    opensuse-leap | opensuse-tumbleweed)
      ;;
    alpine)
      $sudo_flag apk add --no-cache fish
      ;;
    darwin*)
      brew install fish
      ;;
    cygwin | msys2 | win32)
      pacman -Syy --noprogressbar --noconfirm --needed fish
      ;;
    *)
      printf '%s\n' "Unknown OS detected, aborting..." >&2
      ;;
  esac
  # 从GitHub克隆源代码
  git clone https://github.com/fish-shell/fish-shell.git
  # 进入下载来的文件
  cd fish-shell
  # 建立文件夹并切换路径
  mkdir buld; cd build
  # 配置
  # 你可以用cmake的-DCMAKE_INSTALL_PREFIX参数修改安装路径
  # 默认安装在/usr/local下
  cmake ..
  # 编译
  make
  # 安装
  $sudo_flag  make install
}

install_powershell(){
  info_green "install powershell on $lsb_dist now!!!"
}

install_vim(){
  info_green "install vim on $lsb_dist now!!!"
}

install_neovim(){
  info_green "install neovim on $lsb_dist now!!!"
}

install_emacs(){
  info_green "install emacs on $lsb_dist now!!!"
}

install_helix(){
  info_green "install helix on $lsb_dist now!!!"
}

install_vscode(){
  info_green "install vscode on $lsb_dist now!!!"
}

install_nushell(){
  info_green "install nushell on $lsb_dist now!!!"
}

install_dotnet(){
  info_green "install dotnet-sdk on $lsb_dist now!!!"
}



# # -----------------------------account_setting----------------------------------------
# # usermod --shell /bin/bash root  https://www.cnblogs.com/Hackerman/p/16063299.html
# # echo "password" | passwd  root --stdin > /dev/null 2>&1
# # chsh -s /usr/bin/zsh
# echo root:root|chpasswd && usermod --shell /bin/zsh root
# mkdir -p /root/code

# # # create user
# # ENV HOME /home/lgf
# # # RUN groupadd -r lgf && useradd -r -m -g  lgf lgf
# # groupadd -r lgf
# # useradd --create-home --home-dir $HOME -r -g lgf lgf
# # echo "lgf:lgf" | chpasswd
# # echo "lgf ALL=(ALL)  NOPASSWD:ALL" >> /etc/sudoers
# username=lgf
# # useradd -d /home/$username -s /bin/zsh -m $username #用户生成在/home
# useradd -m -s /bin/bash -G sudo,root $username # ubuntu
# # useradd -m -s /bin/zsh -G wheel $username # centos
# # echo $ username"123" | paswd --stdin $username
# echo "$username:$username" | chpasswd  #设置默认密码
# echo "User $username Create Success!"
# echo "$username ALL=(ALL)  NOPASSWD:ALL" >> /etc/sudoers
# mkdir -p /home/$username
# mkdir -p /home/$username/code


## ----------------------------git settings-------------------------------------
info_green "setup ssh"
if [ -f ~/.ssh/github_id_rsa ]; then
  info_red "ssh has been setup!!!"
else
  info_green "setup ssh for $lsb_dist now!!!"
  export GIT_SSL_NO_VERIFY=1
  rm -rf ~/.ssh
  rm -rf ~/git_settings
  cd ~
  git clone https://gitee.com/lgf1244/git_settings.git
  cd ~/git_settings/.ssh
  chmod 600 *_id_rsa
  chmod 600 config
  cp -rp ~/git_settings/.ssh ~
  rm -rf rp ~/git_settings
  cd ~
  git clone git@gitee.com:lgf1244/ssh.git
  rm -rf ~/.ssh
  mv ~/ssh ~/.ssh
  cd ~/.ssh
  chmod 600 config authorized_keys *_rsa
  chmod 644 *.pub
fi

## ----------------------------pip/pipx settings-------------------------------------

if command_exists pip; then
  pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
else
  info_blue "no pip command found, please install it first!!!"
fi

## ----------------------------conda settings-------------------------------------

## ----------------------------npm settings-------------------------------------


## ----------------------------rust settings-------------------------------------


## ----------------------------asdf settings-------------------------------------

## ----------------------------pyenv settings-------------------------------------

## ----------------------------poetry settings-------------------------------------

## ----------------------------virtualfish settings-------------------------------------

## ----------------------------rbenv settings-------------------------------------

## ----------------------------nodenv settings-------------------------------------

# ------------------------install oh-my-zsh------------------------

# git clone https://github.com/ohmyzsh/ohmyzsh.git && ./ohmyzsh/tools/install.sh && \
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting && \
# git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions && \
# git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k && \
# git clone https://github.com/paulirish/git-open.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/git-open && \
# sed -i 's/^plugins=(/plugins=(docker docker-compose python npm autojump zsh-autosuggestions zsh-syntax-highlighting z /' ~/.zshrc && \
# sed -i 's/^ZSH_THEME="robbyrussell"/ZSH_THEME="juanghurtado"/' ~/.zshrc

info_green "install oh-my-zsh now!!!"
if [ -d ~/.oh-my-zsh ]; then
  info_red "already installed oh-my-zsh, will skip it"
else
  rm -rf ./ohmyzsh
  git clone https://gitee.com/studyzy/ohmyzsh.git
  ZSH= sh ./ohmyzsh/tools/install.sh
  git clone https://gitee.com/Kurt-Code/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  git clone https://gitee.com/Kurt-Code/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  git clone https://gitee.com/yanzhongqian/git-open.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/git-open
  git clone https://gitee.com/lgf1244/packer-zsh-completion.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/packer
  sed -i 's/^plugins=(/plugins=(docker docker-compose python npm zsh-autosuggestions zsh-syntax-highlighting z packer /' ~/.zshrc
  sed -i 's/^ZSH_THEME="robbyrussell"/ZSH_THEME="juanghurtado"/' ~/.zshrc
  rm -rf ./ohmyzsh
  echo finished install oh-my-zsh
fi


# ------------------------install oh-my-bash------------------------
# git clone https://github.com/ohmybash/oh-my-bash.git && bash ./oh-my-bash/tools/install.sh
info_green "install oh-my-bash now!!!"
if [ -d ~/.oh-my-bash ]; then
  info_red "already installed oh-my-bash, will skip it"
else
  rm -rf ./oh-my-bash
  git clone https://gitee.com/oh-my-shell/oh-my-bash.git
  bash ./oh-my-bash/tools/install.sh
  rm -rf ./oh-my-bash
  echo finished install oh-my-bash
fi

# ------------------------install oh-my-fish------------------------
# support os: ubuntu-16.04+ debian10+
# git clone https://github.com/oh-my-fish/oh-my-fish.git  && ./oh-my-fish/bin/install --offline --noninteractive
if [ -x /usr/bin/fish ]; then
  info_green "install oh-my-fish now!!!"
  if [ -d ~/.local/share/omf ]; then
    info_red "already installed oh-my-fish, will skip it"
  else
    rm -rf ./oh-my-fish
    git clone https://gitee.com/lgf1244/oh-my-fish.git
    ./oh-my-fish/bin/install --offline --noninteractive
    rm -rf ./oh-my-fish
    echo finished install oh-my-fish
  fi
else
  info_red "not install fish on $lsb_dist distribution"
fi

# ------------------------install oh-my-tmux------------------------

# $ cd
# $ git clone https://github.com/gpakosz/.tmux.git
# $ ln -s -f .tmux/.tmux.conf
# $ cp .tmux/.tmux.conf.local .


info_green "install oh-my-tmux now!!!"
if [ -d ~/.tmux ]; then
  info_red "already installed oh-my-tmux, will skip it"
else
  git clone https://gitee.com/lgf1244/tmux.git ~/.tmux
  ln -s -f ~/.tmux/.tmux.conf ~/.tmux.conf
  cp ~/.tmux/.tmux.conf.local ~
  echo finished install oh-my-tmux
fi


# ------------------------install oh-my-posh------------------------
info_green "install oh-my-posh now!!!"
if [ -d ~/.oh-my-posh ]; then
  info_red "already installed oh-my-posh, will skip it"
else
  echo finished install oh-my-posh
fi

#  ------------------------install cheat.sh---------------------------
if [ -x /usr/local/bin/cht.sh ]; then
  info_sky "cheat.sh has been installed"
  # cht.sh js parse json
  # curl cht.sh/js/parse+json
else
  curl -s https://cht.sh/:cht.sh | $sudo_flag tee /usr/local/bin/cht.sh && $sudo_flag chmod +x /usr/local/bin/cht.sh
fi

# ------------------------install vim-fast---------------------------
# supprt os: Ubuntu-20.04+ Debian-10+
# WARNING:vim is too old,so it can support some feature!  支持8.0以上的版本
# https://gitee.com/mirrorvim/vim-fast
# if [ -f ~/vimrc-no-plug ]; then
if [ -L  ~/.vimrc ] && [ $(readlink ~/.vimrc) == ~/vimrc-no-plug ]; then
  info_red "fast-vim has been setup"
else
  info_green "setup fast-vim has now!!!"
  # curl https://gitee.com/mirrorvim/vim-fast/raw/master/shell/websimple.sh | bash
  cd ~
  rm -f ~/.vimrc
  curl -o vimrc-no-plug https://gitee.com/mirrorvim/vim-fast/raw/master/vimrc-no-plug
  ln -s -f ~/vimrc-no-plug ~/.vimrc
fi
# ------------------------install spacevim---------------------------

# ------------------------install spacemacs---------------------------

# ------------------------install doom-emacs---------------------------


# ------------------------install helix---------------------------


wsl_conf="$(cat \
<<'EOF'
[boot]
systemd=true
[network]
generateResolvConf = false
EOF
)"
# echo "${wsl_conf}" > /etc/wsl.conf

resolv_conf="$(cat \
<<'EOF'
nameserver 192.168.8.1
EOF
)"

# Administrator in ~ > wsl -l -v
#   NAME                                   STATE           VERSION
# * Ubuntu                                 Running         2
#   SUSE-Linux-Enterprise-Server-15-SP4    Stopped         2
#   Debian                                 Stopped         2
#   openSUSE-Leap-15.4                     Stopped         2
#   CentOS6                                Stopped         2
#   Manjaro                                Stopped         2
#   OracleLinux_7_9                        Stopped         2
#   kali-linux                             Stopped         2
#   OracleLinux_8_6                        Stopped         2
#   Rocky8                                 Stopped         2
#   CentOS7                                Stopped         2
#   Deepin                                 Stopped         2
#   OracleLinux_9                          Stopped         2
#   openEuler-22.03                        Stopped         2
#   AlmaLinux9                             Running         2
#   CentOS9-stream                         Stopped         2
#   CentOS8-stream                         Stopped         2
#   openSUSE-Tumbleweed                    Stopped         2
#   AlmaLinux-8                            Stopped         2
#   Distrod                                Stopped         2
#   Ubuntu-18.04                           Stopped         2
#   Alpine                                 Stopped         2
#   Ubuntu-20.04                           Running         2
#   CentOS8                                Stopped         2

wsl2_ip_fixed(){
echo "wsl2_ip_fixed"
wsl2_dist=""
case "$lsb_dist" in
  ubuntu)
    if [ "$sub_lsb_dist" = "16.04" ]; then
      wsl2_dist="Ubuntu-16.04"
    elif [ "$sub_lsb_dist" = "18.04" ]; then
      wsl2_dist="Ubuntu-18.04"
    elif [ "$sub_lsb_dist" = "20.04" ]; then
      wsl2_dist="Ubuntu-20.04"
    elif [ "$sub_lsb_dist" = "22.04" ]; then
      wsl2_dist="Ubuntu"
    else
      wsl2_dist="Ubuntu"
    fi
    ;;
  kali)
    wsl2_dist="kali-linux"
    ;;
  *)
    wsl2_dist=$lsb_dist
    ;;
esac


cat << EOF > ~/wsl2_ip_fixed.bat
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
wsl -d $wsl2_dist  -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d $wsl2_dist  -u root ip addr add 192.168.8.2/24 broadcast 192.168.8.255 dev eth0
wsl -d $wsl2_dist  -u root ip route add 0.0.0.0/0 via 192.168.8.1 dev eth0
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.8.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.8.0/24;"
exit
EOF

}
# echo "${resolv_conf}" > /etc/resolv.conf

# cat << 'EOF' >> /etc/zsh/zprofile
# if [ -d /etc/profile.d ]; then
#   for i in /etc/profile.d/*.sh; do
#     if [ -r $i ]; then
#       . $i
#     fi
#   done
#   unset i
# fi
# EOF
# cat /etc/zsh/zprofile


# # code shim, it fallbacks to code-insiders if code is not available
# cat << 'EOF' > /usr/local/bin/code
# #!/bin/sh

# get_in_path_except_current() {
#     which -a "$1" | grep -A1 "$0" | grep -v "$0"
# }

# code="$(get_in_path_except_current code)"

# if [ -n "$code" ]; then
#     exec "$code" "$@"
# elif [ "$(command -v code-insiders)" ]; then
#     exec code-insiders "$@"
# else
#     echo "code or code-insiders is not installed" >&2
#     exit 127
# fi
# EOF
# chmod +x /usr/local/bin/code


# 如何探测虚拟环境是物理机、虚拟机还是容器？ https://www.infoq.cn/article/536l*xprudowckitdgm4
# To change your login shell to fish:
# Add the shell to /etc/shells with:
# echo /usr/local/bin/fish | sudo tee -a /etc/shells

# Change your default shell with:
# chsh -s /usr/local/bin/fish

if [ -f /.dockerenv ]; then
  info_sky "this is docker environment"
  chsh -s /bin/zsh
  echo "docker-$lsb_dist" | $sudo_flag  tee /etc/hostname
else
  info_sky "this is none-docker environment"
  # chsh: PAM: Authentication failure
  # echo "123456" | chsh -s /bin/zsh
  $sudo_flag usermod --shell /bin/zsh $USER
  if [[ $(uname -a) == *WSL2* ]]; then
    info_green "WSL2 settings"
    echo "wsl2-$lsb_dist" | $sudo_flag  tee /etc/hostname
    # WSL 2 上启用微软官方支持的 systemd https://www.cnblogs.com/wswind/p/wsl2-official-systemd.html
    info_green "enable systemd on wsl2"
    if [ -f /etc/wsl.conf ]; then
      $sudo_flag mv /etc/wsl.conf /etc/wsl.conf.bak
    fi
    echo -e "[boot]\nsystemd=true" | $sudo_flag tee /etc/wsl.conf
    # echo "${wsl_conf}" | $sudo_flag tee /etc/wsl.conf
    ps --no-headers -o comm 1
    cat /etc/wsl.conf

    # if [ -f /etc/resolv.conf ]; then
    #   $sudo_flag mv /etc/resolv.conf /etc/resolv.conf.bak
    # fi
    # echo "${resolv_conf}" | $sudo_flag tee /etc/resolv.conf
    # cat /etc/resolv.conf

    wsl2_ip_fixed

  elif [[ $(uname -a) == *MSYS* ]]; then
    info_green "msys2 settings"
  else
    info_green "none WSL2 settings"
    echo "$lsb_dist" | $sudo_flag  tee /etc/hostname
  fi
fi



## ----------------------------dotfiles settings-------------------------------------

if [ -d ~/.dotfiles ]; then
  info_blue "dotfiles has been download before, please update it now!!!"
  cd ~/.dotfiles && git pull && cd ~
else
  info_blue "will downloas dotfiles now!!!"
  git clone git@gitee.com:lgf1244/dotfiles.git ~/.dotfiles
fi

# ls -l ~/codes | awk -F'->' '{print $2}' | tr -d ' '
# readlink ~/codes

if [ -L  ~/.zshrc ] && [ $(readlink ~/.zshrc) == ~/.dotfiles/.zshrc ]; then
  echo "zshrc softlink has been setup!!!"
else
  echo "setup zshrc softlink now!!!"
  # mv ~/.zshrc ~/.zshrc.bak
  rm -f ~/.zshrc
  ln -s -f ~/.dotfiles/.zshrc  ~/.zshrc
fi

if [ -L  ~/.bashrc ] && [ $(readlink ~/.bashrc) == ~/.dotfiles/.bashrc ]; then
  echo "bashrc softlink has been setup!!!"
else
  echo "setup bashrc softlink now!!!"
  # mv ~/.bashrc ~/.bashrc.bak
  rm -f ~/.bashrc
  ln -s -f ~/.dotfiles/.bashrc  ~/.bashrc
fi

if [ -L  ~/.gitconfig ] && [ $(readlink ~/.gitconfig) == ~/.dotfiles/.gitconfig ]; then
  echo "gitconfig softlink has been setup!!!"
else
  echo "setup gitconfig softlink now!!!"
  rm -f ~/.gitconfig
  ln -s -f ~/.dotfiles/.gitconfig  ~/.gitconfig
fi

# cat << 'EOF' >> /etc/zsh/zshrc
# source /usr/share/autojump/autojump.sh
# source ~/.dotfiles/.alias.sh
# source ~/.dotfiles/.exports.sh
# source ~/.dotfiles/.functions.sh
# EOF
# cat /etc/zsh/zshrc


if [ -f ~/.hushlogin ]; then
  info_red "~/.hushlogin file has been touched"
else
  touch ~/.hushlogin
fi

ssh -T git@gitlab.com



# WSL2:
# choco install lxrunoffline -y    https://www.jianshu.com/p/f0a76c984d30
# ARCH: https://github.com/yuk7/ArchWSL  https://www.programminghunter.com/article/53111205849/
# MANJARO https://github.com/sileshn/ManjaroWSL2
# CENTOS https://github.com/mishamosher/CentOS-WSL  https://www.modb.pro/db/499982


# test it on docker
# curl https://gitee.com/lgf1244/scripts/raw/main/shell/system-init.sh | bash

# docker run -it --rm --name ubuntu-2004  ubuntu:20.04 bash
# docker run -it --rm --name debian-11  debian:11 bash
# docker run -it --rm --name kali  kalilinux/kali-rolling:latest bash
# docker run -it --rm --name fedora  fedora:latest bash
# docker run -it --rm --name centos  centos:latest bash
# docker run -it --rm --name centos-stream-8  dokken/centos-stream-8:latest bash
# docker run -it --rm --name almalinux  almalinux:latest bash
# docker run -it --rm --name rockylinux-91  rockylinux:9.1 bash
# docker run -it --rm --name oraclelinux-9  oraclelinux:9 bash
# docker run -it --rm --name opensuse-leap-154  opensuse/leap:15.4 bash
# docker run -it --rm --name opensuse-tumbleweed  opensuse/tumbleweed:latest bash
# docker run -it --rm --name  nixos nixos/nix:latest bash
# docker run -it --rm --name  alpine-317 alpine:3.17 sh
# docker run -it --rm --privileged --name  alpine-317 alpine:3.17 sh
# docker run -it --rm --name manjarolinux  manjarolinux/base:latest bash
# docker run -it --rm --name archlinux  archlinux:latest bash


# apt update &&  apt install -y curl

# cp /etc/apk/repositories /etc/apk/repositories.bak
# sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
# apk add --no-cache bash bash curl

# docker pull continuumio/anaconda3:latest
# docker pull continuumio/miniconda3:latest
# docker pull bitnami/dotnet-sdk:latest
# docker pull python:latest
# docker pull node:latest
# docker pull r-base:latest
# docker pull rust:latest


